package com.wx.md.service.impl;

import com.alibaba.fastjson.JSON;
import com.android.ddmlib.*;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.md.adb.AdbUtils;
import com.wx.md.adb.GenericReceiver;
import com.wx.md.dao.MdacAnDeviceDao;
import com.wx.md.pojo.AtxDeviceInfo;
import com.wx.md.pojo.ElTreeNode;
import com.wx.md.service.AcAnDeviceService;
import com.wx.md.tb.AcAnDevice;
import com.wx.md.utils.HttpUtils;
import com.wx.md.utils.QRCodeUtil;
import com.wx.md.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * (MdacAnDevice)表服务实现类
 *
 * @author wx
 * @since 2021-06-08 16:10:42
 */
@Service("mdacAnDeviceService")
public class AcAnDeviceServiceImpl extends ServiceImpl<MdacAnDeviceDao, AcAnDevice> implements AcAnDeviceService {

    @Autowired
    AdbUtils adbUtils;


    @Override
    public List<ElTreeNode> getDevicesTree() {
        List<ElTreeNode> elTreeNodeList = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        List<AcAnDevice> acAnDeviceList = super.list(queryWrapper);
        for(AcAnDevice acAnDevice:acAnDeviceList){
            ElTreeNode elTreeNode = new ElTreeNode();
            elTreeNode.setId(acAnDevice.getId());
            elTreeNode.setAgentUrl(acAnDevice.getAgentUrl());
            elTreeNode.setLabel(acAnDevice.getDeviceName()+"_"+acAnDevice.getDeviceIp());
            elTreeNodeList.add(elTreeNode);
        }
        return elTreeNodeList;
    }

    @Override
    public AtxDeviceInfo atxInit(AcAnDevice acAnDevice) {
        String dir = System.getProperty("user.dir");
        String atxAgentPath = dir + "\\open_source\\lib\\atx-agent-0.10.0\\";
        String minicapPath = dir + "\\open_source\\lib\\stf-binaries-0.3.0\\node_modules\\minicap-prebuilt\\prebuilt\\x86\\bin\\minicap";
        String minicapsoPath = dir + "\\open_source\\lib\\stf-binaries-0.3.0\\node_modules\\minicap-prebuilt\\prebuilt\\x86\\lib\\android-25\\minicap.so";
        String minitouchPath = dir + "\\open_source\\lib\\stf-binaries-0.3.0\\node_modules\\minitouch-prebuilt\\prebuilt\\x86\\bin\\";
        System.out.println("atxAgentPath:" + atxAgentPath);
        System.out.println("minicapPath:" + minicapPath);
        System.out.println("minicapsoPath:" + minicapsoPath);
        System.out.println("minitouchPath:" + minitouchPath);
        IDevice iDevice = adbUtils.getIDeviceBySn(acAnDevice.getSerialNumber());
        String sdk = iDevice.getProperty("ro.build.version.sdk");
        String abi = iDevice.getProperty("ro.product.cpu.abi");
        System.out.println(sdk);
        System.out.println(abi);
        try {
            GenericReceiver genericReceiver = new GenericReceiver();
            iDevice.executeShellCommand("ls /data/local/tmp/", genericReceiver);
            boolean isExist = genericReceiver.isExist("atx-agent");
            if (isExist) {
                System.out.println("存在直接启动");
                iDevice.executeShellCommand("chmod 755 /data/local/tmp/atx-agent", genericReceiver);
                iDevice.executeShellCommand("/data/local/tmp/atx-agent server -d", genericReceiver);
                iDevice.executeShellCommand("/data/local/tmp/atx-agent server -d --stop", genericReceiver);
            } else {
                System.out.println("不存在，先上传在启动");

                //上传minicap
                iDevice.pushFile(minicapPath, "/data/local/tmp/minicap");
                iDevice.pushFile(minicapsoPath, "/data/local/tmp/minicap.so");
                iDevice.executeShellCommand("chmod 755 /data/local/tmp/minicap", genericReceiver);
                iDevice.executeShellCommand("chmod 755 /data/local/tmp/minicap.so", genericReceiver);

                //上传minitouch
                iDevice.pushFile(minitouchPath + "minitouch", "/data/local/tmp/minitouch");
                iDevice.executeShellCommand("chmod 755 /data/local/tmp/minitouch", genericReceiver);

                //上传atx-agent
                iDevice.pushFile(atxAgentPath + "atx-agent-386", "/data/local/tmp/atx-agent");
                iDevice.executeShellCommand("chmod 755 /data/local/tmp/atx-agent", genericReceiver);
                iDevice.executeShellCommand("/data/local/tmp/atx-agent server -d", genericReceiver);
                iDevice.executeShellCommand("/data/local/tmp/atx-agent server -d --stop", genericReceiver);
            }
            try {
                String url = "http://" + acAnDevice.getDeviceIp() + ":7912/info";
                String deviceIfo = HttpUtils.doGet(url, null, null);
                System.out.println(deviceIfo);
                AtxDeviceInfo atxDeviceInfo = JSON.parseObject(deviceIfo, AtxDeviceInfo.class);
                System.out.println(atxDeviceInfo.getUdid());
                return atxDeviceInfo;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (AdbCommandRejectedException e) {
            e.printStackTrace();
        } catch (ShellCommandUnresponsiveException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SyncException e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public boolean save(AcAnDevice entity) {
        entity.setId(RandomUtils.randomDateId());
        //"http://192.168.31.37:7912/"
        String url = entity.getDeviceIp() + ":7912";
        entity.setAgentUrl(url);
        return super.save(entity);
    }

    @Override
    public boolean updateById(AcAnDevice entity) {
        return super.updateById(entity);
    }

    @Override
    public AcAnDevice getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    @Override
    public IPage<AcAnDevice> page(IPage<AcAnDevice> page, Wrapper<AcAnDevice> queryWrapper) {
        return super.page(page, queryWrapper);
    }
}
